package org.wzl.springframework.factory.support;

import org.wzl.springframework.exception.BeanException;
import org.wzl.springframework.factory.config.BeanDefinition;

import java.lang.reflect.InvocationTargetException;

/**
 * @author 卫志龙
 * @description: 实例化Bean对象
 * @date 2024/1/16 23:00
 */
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {

    @Override
    public abstract BeanDefinition getBeanDefinition(String beanName) throws BeanException;

    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition) {
        //1、创建Bean对象
        Object bean;
        try {
            bean = beanDefinition.getBeanClass().getConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //2、将bean信息放入容器中
        registerSingleton(beanName, bean);

        return bean;
    }
}
